package com.bjmashibing.system.huawei.vivo;

import com.bjmashibing.system.huawei.jdbc.SpringJdbcTemplate;
import com.bjmashibing.system.huawei.static_var;
import com.xiaomi.xmpush.server.Constants;
import com.xiaomi.xmpush.server.Message;
import com.xiaomi.xmpush.server.Result;
import com.xiaomi.xmpush.server.Sender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Classname xiaomi_push
 * @Description TODO
 * @Date 2020/10/27/027 17:39
 * @Created by Administrator
 */
public class xiaomi_push {
    private final static Logger logger = LoggerFactory.getLogger(xiaomi_push.class);
    public static void sendMessageToAlias2(String alias_temp) throws Exception {

        Constants.useOfficial();
//        Sender sender = new Sender("UvLVj/+EWyqX9dWzY7+a6Q==");
        Sender sender = new Sender("woTsguUC/wC2U1c0nf8BKQ==");
        String messagePayload = "This is a message";
        String title = "notification title";
        String description = "notification description";
        String alias = alias_temp;    //alias非空白, 不能包含逗号, 长度小于128
        Message message = new Message.Builder()
                .title(title)
                .description(description).payload(messagePayload)
                .restrictedPackageName("com.hengbo.phone3")
                .notifyType(1)     // 使用默认提示音提示
                .build();
        sender.sendToAlias(message, alias, 3); //根据alias, 发送消息到指定设备上
    }
    public static void sendMessageToAlias3(String alias_temp,String str_alarm_event,String str_machine,String str_number,String str_data_pure) throws Exception {
        try{
            Constants.useOfficial();
//        Sender sender = new Sender("UvLVj/+EWyqX9dWzY7+a6Q==");
            Sender sender = new Sender("woTsguUC/wC2U1c0nf8BKQ==");
            String messagePayload = str_machine + ":" + str_data_pure;
//        String title = "notification title";
            String title = str_alarm_event;
//        String description = "notification description";
            String description = str_machine + ":" + str_data_pure;
            String alias = alias_temp;    //alias非空白, 不能包含逗号, 长度小于128
            Message message = new Message.Builder()
                    .title(title)
                    .description(description).payload(messagePayload)
                    .restrictedPackageName("com.hengbo.phone3")
//                .extra("callback", "https://boxigarden.cn:8080/hello_xiaomi")
                    .extra("callback", "https://hbpush.com:8080/hello_xiaomi")
                    .notifyType(1)     // 使用默认提示音提示
                    .build();
            Result result = sender.sendToAlias(message, alias, 3); //根据alias, 发送消息到指定设备上

            String MessageId = result.getMessageId();
            Integer ErrorCode_value = result.getErrorCode().getValue();
            String ErrorCode = result.getErrorCode().getDescription();
            String Reason = result.getReason();

//        logger.info("sendMessageToAlias3,MessageId=" + MessageId);
//        logger.info("sendMessageToAlias3,ErrorCode_value=" + ErrorCode_value);
            logger.info("sendMessageToAlias3,xiaomi,ErrorCode=" + ErrorCode + "," + str_machine);
//        logger.info("sendMessageToAlias3,Reason=" + Reason);
            if (ErrorCode_value == 10029) {
                SpringJdbcTemplate springJdbcTemplate = new SpringJdbcTemplate();
                springJdbcTemplate.token_delete(alias_temp);
//            logger.info("sendMessageToAlias3,static_var.map_token=" + static_var.map_token.toString());
                static_var.remove_token_according_to_machine_name(str_machine,alias_temp);
//            logger.info("sendMessageToAlias3,static_var.map_token=" + static_var.map_token.toString());
            }
        }
        catch(Exception e){
            logger.info("sendMessageToAlias3,error=",e.toString());
            e.printStackTrace();
        }

    }
}
